package com.stoneframe.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.stoneframe.controller.base.BaseController;
import com.stoneframe.service.HomeService;
import com.stoneframe.service.manageAuthorize.SysModuleService;
import com.stoneframe.util.baseApp.AppHelper;
import com.stoneframe.util.baseApp.Const;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.ToolHelper;

@Controller
public class HomeController extends BaseController {
	
	@Resource(name="homeService")
	private HomeService homeService;
	
	@Resource(name="sysModuleService")
	private SysModuleService sysModuleService;
	
	@RequestMapping(value="index")
	public ModelAndView index(){
		ModelAndView mv=new ModelAndView();
		mv.addObject("sysName", ToolHelper.readFile(Const.SysName));
		mv.setViewName("home/index");
		return mv;
	}
	
	@RequestMapping(value="welcome")
	public ModelAndView welcome(){
		logBefore(logger, "HomeController.welcome");
		
		ModelAndView mv=this.getModelAndView();
		mv.setViewName("home/welcome");
		
		logAfter(logger);
		return mv;
	}
	
	@RequestMapping(value="listModule")
	@ResponseBody
	public Object listModule(){
		logBefore(logger, "HomeController.listModule");
		PageData pd=this.getPageData();
		List<PageData> list=new ArrayList<PageData>();
		try{
			list=sysModuleService.listAll(pd);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject(e.getMessage());
		}
		logAfter(logger);
		return AppHelper.successObject(list);
	}
	
	/*
	@ResponseBody  
    @RequestMapping(value="/abc/{username}/{resourceUrl}")  
    public String dubboMock(@PathVariable String username,@PathVariable String resourceUrl){  
          return username+"/"+resourceUrl;
    } 
    */
	/**
	 * jsp页面跳转
	 * */
	@RequestMapping(value="jsp/**")
	public ModelAndView jsp(HttpServletRequest request){
		logBefore(logger, "HomeController.jsp");
		
		String url = request.getRequestURI(); 
		int beginIndex=url.toLowerCase().indexOf("/jsp/")+5;
		int endIndex=url.toLowerCase().indexOf(".do");
		String resUrl=url.substring(beginIndex, endIndex);
		logger.info(resUrl);
		PageData pd=this.getPageData();
		ModelAndView mv=this.getModelAndView();
		mv.setViewName(resUrl);
		mv.addObject(pd);
		logAfter(logger);
		return mv;
	}
	
	/**
	 * jsp页面跳转
	 * */
	@ResponseBody
	@RequestMapping(value="control/**",produces= "text/plain;charset=UTF-8")
	public Object control(HttpServletRequest request){
		logBefore(logger, "HomeController.control");
		
		String url = request.getRequestURI(); 
		int beginIndex=url.toLowerCase().indexOf("/control/")+9;
		int endIndex=url.toLowerCase().indexOf(".do");
		String resUrl=url.substring(beginIndex, endIndex);
		String mapper=resUrl.replaceAll("/", ".");
		logger.info(mapper);
		PageData pd=this.getPageData();
		try{
			Object obj=homeService.listAll(mapper, pd);
			logAfter(logger);
			return AppHelper.successObject(obj);
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AppHelper.errorObject(e.getMessage());
		}
	}
	

}
